<template>
  <confirm-dialog
    :title="title"
    :visible="visible"
    :loading="loading"
    @handleClose="handleClose"
    width="25%"
  >
    <template slot="content">
      <p>{{ content }}</p>
      <ul v-if="Array.isArray(data) && action !== 'delete'">
        <li v-for="(item, index) in data" :key="index">{{ item.orderId }}</li>
      </ul>
      <ul v-if="!Array.isArray(data) && action !== 'delete'">
        <li>{{ data.orderId }}</li>
      </ul>
      <div v-if="action === 'retry'"
        style="font-size: 12px; color: #333; line-height: 1.5;"
      >
        <span>{{$t('order.retryPoint')}}</span><br />
        <span style="display:inline-block; margin-left:12px">{{$t('order.orderInstanceDialog.InstanceRetryStop')}}</span
        ><br />
      </div>
    </template>
    <template slot="footer">
      <el-button @click="handleClose">{{ $t("common.cancel") }}</el-button>
      <el-button :disabled="loading" type="primary" @click="submit">{{
        $t("common.confirm")
      }}</el-button>
    </template>
  </confirm-dialog>
</template>
<script lang="ts">
import Vue from "vue";
import FormDialop from "../../../../components/Dialog/formDialog.vue";
import ConfirmDialog from "../../../../components/Dialog/confirmDialog.vue";
import orinusApi from '../../../../api/orinusApi';
export default Vue.extend({
  components: {
    FormDialop,
    ConfirmDialog,
  },
  props: {
    submitMethod: {
      type: Function,
      default() {
        return (formData: any) => Promise.resolve();
      },
    },
  },
  data() {
    return {
      title: "",
      visible: false,
      loading: false,
      content: "",
      data: {},
      action: "",
      categoryCode: 2,
    };
  },
  methods: {
    handleClose() {
      this.visible = false;
      this.loading = false;
    },
    async init(obj: any) {
      this.data = obj.data as any;
      this.action = obj.type;
      this.orderType = obj.orderType;
      this.categoryCode = obj.categoryCode;
      switch(obj.type) {
        case 'retry':
          this.title = (this as any).$t('order.orderInstance.retryInstance');
          if (obj.data.length > 1) {
            this.content = (this as any).$t('order.orderInstanceDialog.InstanceRetryBatch');
          } else {
            this.content = (this as any).$t('order.orderInstanceDialog.InstanceRetryOnly');
          };
        break;
        case 'confirmFail':
          this.title = (this as any).$t('order.confirmFail').toString();
          if (obj.data.length > 1) {
            this.content = (this as any).$t('order.batchConfirmFailOrder').toString();
          } else {
            this.content = (this as any).$t('order.confirmFailOrder').toString();
          };
        break;
        case 'delete':
          if (obj.data.length > 1) {
            this.title = (this as any).$t('common.BatchDelete').toString();
            this.content = (this as any).$t('order.batchDeleteOrder').toString();
          } else {
            this.title = (this as any).$t('common.delete').toString();
            this.content = (this as any).$t('order.deleteOrder').toString();
          };
      }
      this.visible = true;
    },
    submit() {
      this.loading = true;
      let promiseArray: any = [];
      if (this.action === 'retry') { // 重试
        const data: any = [];
        if(Array.isArray(this.data)) {
          (this.data as any).forEach((item: any) => {
            const obj: any = {};
            obj.categoryCode = (this as any).categoryCode;
            obj.orderId = item.orderId;
            obj.status = item.status;
            obj.step = item.step;
            obj.requestType = item.requestType;
            obj.requestUUID = item.requestUUID;
            obj.instanceId = item.instanceId;
            data.push(obj);
          })
        } else {
          const obj: any = {};
            obj.categoryCode = (this as any).categoryCode;
            obj.orderId = (this.data as any).orderId;
            obj.status = (this.data as any).status;
            obj.step = (this.data as any).step;
            obj.requestType = (this.data as any).requestType;
            obj.requestUUID = (this.data as any).requestUUID;
            obj.instanceId = (this.data as any).instanceId;
            data.push(obj);
        }
        (this as any).$orsApi.doTryBatchOrders(data).then((res: any) => {
          this.loading = false;
          this.visible = false;
          this.$emit("success");
          this.$message({
            message: (this as any).$t('order.orderInstanceDialog.RetrySuccess'),
            type: "success",
          });
        }).catch((error: any) => {
          this.loading = false;
          this.$message({
            message: (this as any).$t('order.orderInstanceDialog.RetryFailed'),
            type: "warning",
          });
        })
      } else { // 确认失败
        let array: any = [];
        if (Array.isArray(this.data)) {
          (this.data as any).forEach((item: any) => {
            const obj: any = {};
            obj.categoryCode = (this as any).categoryCode;
            obj.requestUUID = item.requestUUID;
            obj.orderId = item.orderId;
            obj.status = item.status;
            obj.step = item.step;
            obj.requestType = item.requestType;
            obj.instanceId = item.instanceId;
            array.push(obj);
          })
        } else {
          const obj: any = {};
          obj.categoryCode = (this as any).categoryCode;
          obj.requestUUID = (this as any).data.requestUUID;
          obj.orderId = (this as any).data.orderId;
          obj.status = (this as any).data.status;
          obj.step = (this as any).data.step;
          obj.requestType = (this as any).data.requestType;
          obj.instanceId = (this as any).data.instanceId;
          array.push(obj);
        }
        
        (this as any).$orsApi.batchconfirmFailedOrders(array).then((res: any) => {
          this.loading = false;
          this.visible = false;
          this.$emit("success");
          // let errCodes = -1;
          // res.rows.forEach((item: any) => {
          //   if (item.code !== 200) {
          //     errCodes = 0;
          //   }
          // })
          const errCodes = res.rows.findIndex((value: any, index: any) => {
            return value.code != 200;
          });
          if(errCodes == -1) {
            this.$message({
              message: (this as any).$t('message.requestSuccess'),
              type: "success",
            });
          } else {
            this.$emit("batchConfirmFailed",res);
          }
        }).catch((error: any) => {
          console.log(error);
          this.loading = false;
          this.$message({
            message: (this as any).$t('message.actionError'),
            type: "warning",
          });
        })
      }
    },
  },
});
</script>
